package com.example.srm.common.handle;

import com.example.srm.common.exception.MyException;
import com.example.srm.domain.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author pfk
 * @creatTime 2022/01/20 19:05
 * @describe 全局异常处理
 */
@ControllerAdvice
@Slf4j
public class MyExceptionHandler {

    /**
     * 自定义全局异常:返回指定格式数据返回给前端
     * @param e
     * @return
     */
    @ExceptionHandler({MyException.class})
    @ResponseBody
    public Result loginException(MyException e){
        log.error("自定义异常：",e);
        return new Result().setCode(e.getCode()).setMsg(e.getMsg());
    }

}
